package com.wcgksoft.controller.insect;


import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.insect.InsectType;
import com.wcgksoft.service.insect.InsectTypeService;
import net.sf.json.JSONObject;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
  * @Description: 病虫类型查询
  * @Author: dell 
  * @Date: 2020/4/3  
  */  

@Controller
@RequestMapping("insect")
public class InsectTypeController {

    @Autowired
    private InsectTypeService insectTypeService;



    

    @ResponseBody
    @RequestMapping(value = "findInsectType")
    public Layui find(@RequestParam(value = "page", required = false) Integer page,
                      @RequestParam(value = "limit", required = false) Integer limit) {

        DetachedCriteria criteria = DetachedCriteria.forClass(InsectType.class);

        List<InsectType> list = insectTypeService.findByConditions(criteria,page,limit);
        return Layui.data(0, "查询成功", insectTypeService.getTotalCount(), list);
    }


    @ResponseBody
    @RequestMapping(value = "update")
    public Layui update(@RequestParam(value = "data", required = false) String data){
        JSONObject param = JSONObject.fromObject(data);
        if (param == null) {
            return Layui.data(1, "请求格式有误", 0, null);
        } else {

            InsectType insectType = (InsectType) JSONObject.toBean(JSONObject.fromObject(data), InsectType.class);
            insectTypeService.saveOrUpdate(insectType);
            return Layui.data(0, "修改成功", 1, Layui.addList(insectType));
        }
    }


    @ResponseBody
    @RequestMapping(value = "delete")
    public Layui delete(@RequestParam(value = "id") Integer id) {
        InsectType byId = insectTypeService.findById(id);
        insectTypeService.delete(byId);
        return Layui.data(1, "删除成功", 1, null);
    }





}
